//
//  ViewController2.m
//  PASystem
//
//  Created by michael on 16/7/19.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "huisuViewController.h"
#import "ViewController2.h"
#import "OALoginViewController.h"
#import "bumenyewuViewController.h"
#import "daxiangmuTableViewController.h"
@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"浙江省贸促会电子业务系统(PA)";
    short port = 443;                        //vpn设备端口号，一般为443
NSString *vpnIp =    @"122.224.123.170";  //vpn设备IP地址
    _helper = [[AuthHelper alloc] initWithHostAndPort:vpnIp port:port delegate:self];

   //创建返回按钮
   [self _backbutton];
        _array = [NSMutableArray array];
 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;

//    _array = @[@"部门业务",@"大项目管理",@"领导决策",@"会属单位"];
   //创建按钮
  
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
 NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':'getJurisdiction','service':'UserService', 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,name:前端栏目}'}",userId,pwdId,userHumber];
     NSArray * array = @[@{@"arg0":json}];

     [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
     NSArray *array0 = JSON[@"reason"];
     NSDictionary *dic = [array0 firstObject];
    NSArray *array = dic[@"value"];
    for (NSDictionary *dic in array) {
    NSString *string = dic [@"name"];
    [_array addObject:string];
}
         [self _button];
         NSLog(@"%@",JSON);
         
         

    } failure:^(NSError *error) {
        
    }];
  

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)_backbutton{
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 44, 44);
[rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:@"退出" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                 
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    UIButton *liftButton = [UIButton buttonWithType:UIButtonTypeCustom];
liftButton.frame = CGRectMake(0, 0,95*.6, 72*.6);
[liftButton setImage:[UIImage imageNamed:@"标题logo"] forState:UIControlStateDisabled];
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:liftButton];
 liftButton.enabled = NO;
  
}
- (void)rightButtonAction:(UIButton *)button {

 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"是否退出登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

if (buttonIndex == 1){



[_helper logoutVpn];

//
// OALoginViewController * loginVC =  [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
     [[NSUserDefaults standardUserDefaults] setObject:@"N"forKey:@"autoLogin"];
         UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

//     loginVC.type = @"1";
//    self.view.window.rootViewController = loginVC;
     OALoginViewController * loginVC =  [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
     [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
    self.view.window.rootViewController = loginVC;
//abort();
}
}

- (void)_button{
    float width = (kScreenWidth-90)/2.0;
    float height = (KScreenHeight-270-46)/2.0;
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake(width, height);
       flowlayout.sectionInset = UIEdgeInsetsMake(60, 30, 40, 30);

    flowlayout.minimumInteritemSpacing = 20;
    flowlayout.minimumLineSpacing = 40;
 _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:flowlayout];
  _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.backgroundView = nil;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];

    [self.view addSubview:_collectionView];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString * CellIdentifier = @"GradientCell";  

    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    UIImageView *image = [[UIImageView alloc]initWithFrame:cell.contentView.frame];
    image.image = [UIImage imageNamed:_array[indexPath.row]];
    [cell.contentView addSubview:image];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

NSLog(@"%@",_array[indexPath.row]);
if ([_array[indexPath.row] isEqualToString: @"部门管理"]) {
    bumenyewuViewController *vc = [[bumenyewuViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
if ([_array[indexPath.row] isEqualToString: @"会属单位管理"]) {
//huisuViewController
  huisuViewController *vc = [[huisuViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
if ([_array[indexPath.row] isEqualToString: @"大项目管理"]) {
//huisuViewController
  daxiangmuTableViewController *vc = [[daxiangmuTableViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}

}
@end
